package ui.activity;

import android.Manifest;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.fy.fyzf.R;
import com.fy.fyzf.base.BaseActivity;
import com.fy.fyzf.base.BasePresenter;
import com.fy.fyzf.dialog.LoopViewPopwindow;
import com.fy.fyzf.listener.PermissionListener;
import com.fy.fyzf.utils.AppUtils;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class RecommendResultActivity extends BaseActivity {


    @BindView(R.id.tv_contact)
    TextView tvContact;
    @BindView(R.id.tv_countiue)
    TextView tvCountiue;
    private String phone;

    @Override
    protected BasePresenter createPresenter() {
        return null;
    }

    @Override
    protected int provideContentViewId() {
        return R.layout.activity_suggest_housing;
    }

    private List<String> stringList = new ArrayList<>();
    private List<Integer> idList = new ArrayList<>();

    @Override
    public void initView() {
        super.initView();
        stringList.add("电话");
        stringList.add("短信");

        idList.add(0);
        idList.add(0);
    }

    @Override
    public void initData() {
        super.initData();
        phone = getIntent().getStringExtra("phone");
    }

    @OnClick({R.id.tv_contact, R.id.tv_countiue})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_contact:
                showDialog(stringList);
                break;
            case R.id.tv_countiue:
                finish();
                break;
        }
    }

    private void showDialog(List<String> titles) {
        LoopViewPopwindow loopViewPopwindow = new LoopViewPopwindow(this);
        loopViewPopwindow.setIds(idList).setTitleData(titles).setTitle("").showPop(tvContact);
        loopViewPopwindow.getView(R.id.tv_sure).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                loopViewPopwindow.dismiss();
                if (loopViewPopwindow.position == 0) {
                    toCall(phone);
                } else {
                    sendSMS(phone);
                }
            }
        });
    }

    private void sendSMS(String smsBody) {
        Uri smsToUri = Uri.parse("smsto:");
        Intent intent = new Intent(Intent.ACTION_SENDTO, smsToUri);
        intent.putExtra("sms_body", smsBody);
        startActivity(intent);
    }

    private void toCall(String phone){
        requestRuntimePermission(new String[]{Manifest.permission.CALL_PHONE}, new PermissionListener() {
            @Override
            public void onGranted() {
                Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + phone));
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(intent);
            }
            @Override
            public void onDenied(List<String> deniedPermissions) {
                AppUtils.showToast("拨号权限被拒绝");
            }
        });
    }
}